<?php
namespace app\admin\model;
use think\Model;
use think\Db;
use think\Request;
use think\Session;

class Admin extends Model
{
    public function position(){
        return $this->belongsTo('position','position_id');
    }

	public function login($data){
	    $captcha = new \think\captcha\Captcha();
	    if (!$captcha->check($data['code'])) {
	        return 4;
	    }
        $user=Db::name('admin')->where('name','=',$data['name'])->find();
        if($user){
            if($user['password']==md5($data['password'])){
            $res = Db::name('position')->where('id',$user['position_id'])->find();
            $group = Db::name('auth_group_access')->where('uid',$user['id'])->find();
            $groupn=Db::name('auth_group')->where('id',$group['group_id'])->find();
            $login_info=[
                'username'=>$user['name'],
                'uid'=>$user['id'],
                'position_id'=>$res['id'],
                'position_name'=>$res['pname'],
                'position_level'=>$res['level'],
                'area_id'=>$res['area_id'],
                'group_id'=>$group['group_id'],
                'group_name'=>$groupn['title'],
                'session_start_time'=> time()     //记录会话开始时间！判断会话时间的重点！重点！重点！
            ];
            Session::set('logininfo',$login_info);
				return 3;//信息正确
                }else{
	                return 2;//密码不正确
                }
         }
         else{
         	return 1; //用户不存在
         }
	}
	
	
    
}
